//
//  ChangeNavigationBar.m
//  PetAPP
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ChangeNavigationBar.h"
#import "ViewController.h"

void removeNaviSubViews(ViewController *vc) {
    [vc.navigationItem.titleView removeFromSuperview];
    vc.navigationItem.leftBarButtonItem = nil;
    vc.navigationItem.rightBarButtonItem = nil;
}

void createGrayLine(ViewController *vc) {
    CGFloat width = vc.view.frame.size.width / 7;
    vc.redLine = [[UIView alloc] initWithFrame:CGRectMake(width * 2 + 5, 39, width - 10, 5)];
    vc.redLine.layer.borderWidth = 3;
    vc.redLine.layer.borderColor = [UIColor grayColor].CGColor;
    vc.redLine.layer.cornerRadius = 2.5;
    vc.redLine.layer.masksToBounds = YES;
    [vc.navigationController.navigationBar addSubview:vc.redLine];


}

void changeHome(ViewController *vc) {
    removeNaviSubViews(vc);
    /** 把navigetionItem分成7分方便一会调用 */
    CGFloat width = vc.view.frame.size.width / 7;
    
    vc.sg = [[UISegmentedControl alloc] initWithItems:@[@"精选", @"关注", @"最新"]];
    vc.sg.frame = CGRectMake(width * 2, 0, width * 3, 44);
    vc.sg.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
    /** 设置字体选中时颜色 */
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName: [UIColor grayColor]};
    [vc.sg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    /** 设置字体非选中时颜色 */
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName: [UIColor lightGrayColor]};
    [vc.sg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    /** 这只初始选择项为0 */
    vc.sg.selectedSegmentIndex = 0;
    vc.navigationItem.titleView = vc.sg;
    /** 添加事件 */
    [vc.sg addTarget:vc action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [vc.sg release];

    
    /** 设置左侧button属性 */
    vc.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    vc.leftButton.frame = CGRectMake(0, 0, 20, 20);
    [vc.leftButton setBackgroundImage:[UIImage imageNamed:@"saomiao"] forState:UIControlStateNormal];
    [vc.leftButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.leftButton];
    vc.leftButton.tag = 100;
    
    /** 设置右侧button属性 */
    vc.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    vc.rightButton.frame = CGRectMake(0, 0, 20, 20);
    [vc.rightButton setBackgroundImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    [vc.rightButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.rightButton];
    vc.rightButton.tag = 101;
    
    
    vc.redLine.alpha = 1;
   
}

void changeShop(ViewController *vc) {
    vc.redLine.alpha = 0;
    removeNaviSubViews(vc);
    
    /** 设置左侧button属性 */
    vc.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    vc.leftButton.frame = CGRectMake(0, 0, 40, 20);
    [vc.leftButton setTitle:@"分类" forState:UIControlStateNormal];
    [vc.leftButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    vc.leftButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [vc.leftButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.leftButton];
    vc.leftButton.tag = 200;
    
    /** 设置右侧button属性 */
    vc.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    vc.rightButton.frame = CGRectMake(0, 0, 25, 25);
    [vc.rightButton setBackgroundImage:[UIImage imageNamed:@"gouwuche"] forState:UIControlStateNormal];
    [vc.rightButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.rightButton];
    vc.rightButton.tag = 201;
    
    
    UIView *viewOfSearch = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 30)];
    vc.navigationItem.titleView = viewOfSearch;
    [viewOfSearch release];
    
    viewOfSearch.backgroundColor = [UIColor whiteColor];
    viewOfSearch.layer.cornerRadius = 7;
    viewOfSearch.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(handleActionWithTap:)];
    [viewOfSearch addGestureRecognizer:tap];
    [tap release];
    
    
    UIImageView *imageViewOfSearch = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 16, 16)];
    imageViewOfSearch.image = [UIImage imageNamed:@"sousuo"];
    [viewOfSearch addSubview:imageViewOfSearch];
    [imageViewOfSearch release];
    
    
    UILabel *labelOfText = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 150, 20)];
    [viewOfSearch addSubview:labelOfText];
    [labelOfText release];
    labelOfText.text = @"搜索你想要的宝贝";
    labelOfText.font = [UIFont systemFontOfSize:16];
    labelOfText.textColor = [UIColor grayColor];
    
    

}

void changeServer(ViewController *vc) {
    vc.redLine.alpha = 0;
    removeNaviSubViews(vc);
    UILabel *labelOfTitle = [[UILabel alloc] init];
    labelOfTitle.frame = CGRectMake(0, 0, 150, 40);
    labelOfTitle.textAlignment = NSTextAlignmentCenter;
    labelOfTitle.text = @"爱宠服务";
    labelOfTitle.font = [UIFont systemFontOfSize:20];
    vc.navigationItem.titleView = labelOfTitle;
    labelOfTitle.textColor = [UIColor colorWithRed:0.349 green:0.1255 blue:0.1412 alpha:1.0];
    
    
    /** 设置右侧button属性 */
    vc.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    vc.rightButton.frame = CGRectMake(0, 0, 25, 25);
    [vc.rightButton setBackgroundImage:[UIImage imageNamed:@"dianhua"] forState:UIControlStateNormal];
    [vc.rightButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.rightButton];
    vc.rightButton.tag = 301;
    
    
//    /** 设置左侧button属性 */
//    vc.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    vc.leftButton.frame = CGRectMake(0, 0, 40, 20);
//    [vc.leftButton setTitle:@"分类" forState:UIControlStateNormal];
//    [vc.leftButton addTarget:vc action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
//    vc.leftButton.titleLabel.font = [UIFont systemFontOfSize:17];
//    [vc.leftButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
//    
//    
//    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vc.leftButton];
//    vc.leftButton.tag = 300;
    
}


void changeMy(ViewController *vc) {
    vc.redLine.alpha = 0;
    removeNaviSubViews(vc);
    
    UILabel *labelOfTitle = [[UILabel alloc] init];
    labelOfTitle.frame = CGRectMake(0, 0, 150, 40);
    labelOfTitle.textAlignment = NSTextAlignmentCenter;
    labelOfTitle.text = @"我的";
    labelOfTitle.font = [UIFont systemFontOfSize:20];
    vc.navigationItem.titleView = labelOfTitle;
    [UIColor colorWithRed:0.349 green:0.1255 blue:0.1412 alpha:1.0];

    
}


@implementation ChangeNavigationBar

@end
